package com.swlz.servlet;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.swlz.entity.WebFile;
import com.swlz.service.WebFileService;
import com.swlz.service.impl.WebFileServiceForFile;

@SuppressWarnings("serial")
@WebServlet("/down.let")
public class DownServlet extends HttpServlet {
	
	private WebFileService service = new WebFileServiceForFile();
	private Logger log = LoggerFactory.getLogger(this.getClass());

	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException {
		Long id = 0L;
		try{
			id = Long.valueOf(request.getParameter("id"));
		}catch(Exception ex){}
		WebFile webFile = null;
		try{
			webFile = service.get(id);
			String fileName = webFile.getFileName();
			fileName = new String(fileName.getBytes(),"ISO8859-1");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName + "");
			response.sendRedirect(service.getUrl(webFile));
			
		}catch(Exception ex){
			log.debug("错误",ex);
			response.sendError(404);
			return;
		}
	}
}
